package com.adv.controller.base;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.adv.model.AdvertposSqlParam;
import com.adv.model.MessageSqlParam;
import com.adv.service.AdvertposService;
import com.adv.service.MessageService;
import com.adv.service.UserService;
import com.adv.util.Constant;
import com.adv.vo.LoginlogForm;

@Controller
public class MainController extends BaseController {

	@Autowired
	private AdvertposService advertposService;
	@Autowired
	private MessageService messageService;
	@Autowired
	private UserService userService;

	@RequestMapping("/main")
	public String main(ModelMap model) {
		return "main";
	}

	@RequestMapping("/home")
	public String home(ModelMap model) {
		AdvertposSqlParam param = new AdvertposSqlParam();
		param.createCriteria().andEnableEqualTo(true).andTypeEqualTo(Constant.AdverposType.ADVERTISER.getCode());
		model.put("advertiserCount", advertposService.getCountBySelective(param));

		param = new AdvertposSqlParam();
		param.createCriteria().andEnableEqualTo(true).andTypeEqualTo(Constant.AdverposType.PRODUCT.getCode());
		model.put("productCount", advertposService.getCountBySelective(param));

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		Calendar now = Calendar.getInstance();
		String start = sdf.format(now.getTime());
		now.add(Calendar.DAY_OF_MONTH, 1);
		String end = sdf.format(now.getTime());

		param = new AdvertposSqlParam();
		param.createCriteria().andEnableEqualTo(true).andTypeEqualTo(Constant.AdverposType.ADVERTISER.getCode())
				.andRegistertimeBetween(start, end);
		model.put("advertiserToday", advertposService.getCountBySelective(param));

		param = new AdvertposSqlParam();
		param.createCriteria().andEnableEqualTo(true).andTypeEqualTo(Constant.AdverposType.PRODUCT.getCode())
				.andRegistertimeBetween(start, end);
		model.put("productToday", advertposService.getCountBySelective(param));

		MessageSqlParam messageSqlParam = new MessageSqlParam();
		messageSqlParam.createCriteria().andEnableEqualTo(true);
		model.put("messageCount", messageService.getCountBySelective(messageSqlParam));

		now = Calendar.getInstance();
		messageSqlParam = new MessageSqlParam();
		start = sdf.format(now.getTime());
		now.add(Calendar.DAY_OF_MONTH, 1);
		end = sdf.format(now.getTime());
		messageSqlParam.createCriteria().andEnableEqualTo(true).andRegistertimeBetween(start, end);
		model.put("messageToday", messageService.getCountBySelective(messageSqlParam));

		LoginlogForm loginlogForm = userService.getLastLoginlog();
		if (null != loginlogForm) {
			model.put("loginlog", loginlogForm);
		}

		return "home";
	}

	@RequestMapping("/test/add")
	public String list(ModelMap model) {
		return "test/modify";
	}

	@RequestMapping("/test/page")
	public String xx(String page) {
		return "test/" + page;
	}

	@RequestMapping("/nopermission")
	public String nopermission() {
		return "nopermission";
	}

	@RequestMapping("/error")
	public String error() {
		return "error";
	}

	@RequestMapping("/404")
	public String error_404() {
		return "404";
	}

}
